// -*- c++ -*-
// $Header: /home/asanuma/lib/cvsroot/c/gnuplot/gp_plot.cc,v 1.1 2004/10/26 23:27:12 asanuma Exp $

#ifndef lint
static char *RCSid_gp_plot_cc="$Header: /home/asanuma/lib/cvsroot/c/gnuplot/gp_plot.cc,v 1.1 2004/10/26 23:27:12 asanuma Exp $";
#endif

#include <iostream>
#include <iomanip>
#include <vector>
#include "gnuplot.h"

namespace gnuplotpp{

 namespace detail{

 using std::ostream;
 using std::endl;
 using std::vector;
    
 //// class plot ///////////////////

 subplot& plot::_plot( void ){
	 _p.push_back( subplot() );
	 return _p.back();
 }

 subplot& plot::plotfile( void ){ return _plot();}
 subplot& plot::plotfunc( void ){ return _plot();}
	
 void plot::print(ostream &o ) const 
 {
      
	 o << _pre.str();
      
	 if( _p.size()>=1 ){
		 _container::const_iterator it=_p.begin();
		 o << "plot \\" << endl << (it++)->str();
		 for( ; it!=_p.end(); it++ )
			 o << ",\\" << endl << it->str();
	 }
	 o << endl;
      
	 o << _post.str();
      
 }

	void plot::initialize( void ){

		_plotbase::initialize( _pre, _post );
	}
    
 //  ostream & operator << ( ostream &o, const plot &p ){
 // 	p.print( o ); return o;
 //  }

 //// class singleplot ///////////////
 singleplot::singleplot( void ){};
 singleplot::singleplot( const std::string &_s ): 
	 _filebase(_s){};
 singleplot::singleplot( const char *_s ):
	 _filebase(_s){};

 void singleplot::print( ostream &o ) const {
	 plot::print(o);		
 }
 //// class multiplot /////////////////////////////

 multiplot::multiplot( int n ): 
	 _contbase(n,plot()),  _plotbase(_pre,_post)
 {
	 // not best, but compromising solution
	 for( int i=0; i<n; i++ ) (*this)[i].initialize();
 };

	
 multiplot::multiplot( const std::string &_s, int n ): 
	 _contbase(n),  _plotbase(_pre,_post), 
	 _filebase(_s){};

 multiplot::multiplot( const char *_s, int n ): 
	 _contbase(n),  _plotbase(_pre,_post), 
	 _filebase(_s){};

//  multiplot::multiplot( const multiplot & ):  
// 	 _plotbase(_pre,_post){}; // never use!!

 void multiplot::print( ostream &o ) const {
      
	 o << _pre.str();

	 const_iterator it=begin();
	 for( ; it!=end(); it++ ){ o << endl; it->print(o); o << endl;}
		
	 o << _post.str();
		
 }
    
 //// class tableplot ///////////////////////////

 tableplot::tableplot( int nx, int ny ):
	 _contbase(nx,vector<plot>(ny,plot())), _plotbase(_pre,_post),
	 _sizebase()
 {
	 
	 for( int i=0; i<nx; i++ ){
		 for( int j=0; j<ny; j++ )
			 (*this)[i][j].initialize();
	 }
					
	 _nx=nx; _ny=ny;
	 _pre << "## auto generated by tableplot()" << endl;
	 _pre << "n=      "   << nx*ny << endl
				<< "_nx=    "   << nx << "; " 
				<< "_ny=    " << ny << endl
				<< "_xoff=0; _yoff=0" << endl;
	 outsize();
	 _pre << "##" << endl;
	 _p[0][0].buf() << gpset("multiplot") << endl;;
	 _post     << gpset("nomultiplot") << endl;;
	 for( int ix=0; ix<nx; ix++ )
		 for( int iy=0; iy<ny; iy++ )
			 _p[ix][iy].buf() << "####" << endl
												<< "ix=" << ix << "; " 
												<< "iy=" << iy << endl
												<< gpset( "size" " _gxsize,", "_gysize" ) << endl
												<< gpset( "origin", "_dx*ix+_xoff,", 
																	"_dy*(_ny-iy-1)+_yoff" ) << endl;;
	 
 }
  
	// tableplot::tableplot( const tableplot & ): _plotbase(_pre,_post){};

 void tableplot::outsize( void ){
	 _sizebase::outsize();
	 _pre << "## auto generated by tableplot::outsize()" << endl;		
	 _pre << "_dx=_xsize/_nx; " 
				<< "_dy=_ysize/_ny" << endl
				<< "_gxsize=_xsize/_nx; " 
				<< "_gysize=_ysize/_ny" << endl;
	 _pre << "##" << endl;
 }
	
 void tableplot::print( ostream &o ) const {
      
	 o << _pre.str();
		
	 _container::const_iterator it=sub().begin();
	 _subcontainer::const_iterator jt;
	 for( ; it!=sub().end(); it++ )
		 for( jt=it->begin(); jt!=it->end(); jt++ ){
			 jt->print(o); o << endl;;
		 }
	 o << _post.str();
		
 }

 //// class squareplot //////////////

 squareplot::squareplot(void){
	 _pre << "## auto generated by squareplot()" << endl;
	 outsize();
	 _pre << "##" << endl;
	 plotfunc("x") << " t '' lt 1";
 };

 void squareplot::setsize( double x ){
	 _xsize=_ysize=x;
	 outsize();
 }

 void squareplot::outsize( void ){
	 _pre << std::setiosflags( std::ios::fixed ) 
				<< std::setprecision(2)
				<< "_xsize=" << _xsize << endl
				<< "_ysize=" << _ysize << endl
				<< "set size square _xsize, _ysize" << endl;
 }
    	

 }//detail
} // gnuplot

